package DataFile;

import java.nio.ByteBuffer;
import java.util.Formatter;

import ClassData.ConstantPoolField_Info;

public class Structure_CP_FieldInfo extends Structure_CP_Constant {

	private int fieldAddress;	
	private int classAddress;	

	ConstantPoolField_Info myFieldInfo;


	public Structure_CP_FieldInfo(ConstantPoolField_Info myFieldInfo) {
		this.myFieldInfo=myFieldInfo;
		fieldAddress=-1;
	}

	public short getFieldClassIndex() {
		return(myFieldInfo.getNameIndex());
	}
	
	public short getFieldDescriptorIndex() {
		return(myFieldInfo.getDescriptorIndex());
	}
	
	public void setClassAddress(int newClassAdress) {
		classAddress=newClassAdress;
	}
	
	public int getClassAddress() {
		return(classAddress);
	}
	
	public void setFieldAddress(int fieldAdress) {
		this.fieldAddress=fieldAdress;
	}

/*


public int getFileAdress() throws DataFileException
{
if 	(fieldAdress==-1)
	throw new DataFileException(DataFileException.ErrorTypes.ADRESS_NOT_SET);

return(fieldAdress);
}
*/

	public int getMemorySize() {
		int size;
		
		size=BYTE_WIDTH;	// Type
		size+=SHORT_WIDTH;	// class name index
		size+=SHORT_WIDTH;	// descriptor index
		
		size+=ADRESS_WIDTH; // field address
		size+=ADRESS_WIDTH; // class address
	
		return(size);	
	}

	protected String getMemoryMapAsString() {
		Formatter fmt = new Formatter();
		return "" + fmt.format("%s |%14s | size - %3d | name - %3d | descriptor - %3d | %s\n", 
				getAdressAsHexString(baseAdress), 
				"(field info)", 
				getMemorySize(), 
				myFieldInfo.getNameIndex(),
				myFieldInfo.getDescriptorIndex(),
				"field adress - " + getAdressAsHexString(fieldAddress));
	}

	/**
	 *  writes data into memory file
	 *  
	 *  @author Tavin
	 *  
	 *  @param fileData buffer for data 
	 */
	public void writeData(ByteBuffer fileData) {
		fileData.position(baseAdress-DataFileCreator.BASE_MEMORY_ADRESS);
		fileData.put((byte)myFieldInfo.getType());
	
		fileData.putShort((short)myFieldInfo.getNameIndex());
		fileData.putShort((short)myFieldInfo.getDescriptorIndex());
		fileData.putInt(fieldAddress);
		fileData.putInt(classAddress);	
	}

}
